package net.esj.basic.core.engine.tools;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public abstract class NetworkManagement {
	
		private static int htmlCodeSize = 50;
		
		private static String[] urls =new String[] { "http://www.baidu.com",
		"http://www.google.cn","http://www.163.com" };

		private static boolean canGetHtmlCode(String httpUrl) {
			String htmlCode = "";
			try {
				InputStream in;
				URL url = new java.net.URL(httpUrl);
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				connection.setConnectTimeout(5000);
				connection = (HttpURLConnection) url.openConnection();
				connection.setRequestProperty("User-Agent", "Mozilla/4.0");
				connection.connect();
				in = connection.getInputStream();
				byte[] buffer = new byte[htmlCodeSize];
				in.read(buffer);
				htmlCode = new String(buffer);
			} catch (Exception e) {
			}
			if (htmlCode == null || htmlCode.equals("")) {
				return false;
			}
			return true;
		}

		public static boolean  isNetWorkLinked() {
			boolean tempIsNetWorkLinked = false;
			for (int urlsCount = 0; urlsCount < urls.length; urlsCount++) {
				if (canGetHtmlCode(urls[urlsCount])) {
					return true;
				}
			}
			return false;
		}

		public static void main(String[] args) {
			System.out.println(NetworkManagement.isNetWorkLinked());
		}
}

